package com.xiaoshangkou.smalltv2.thread;

import com.xiaoshangkou.smalltv2.commons.publicList;

import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SubThread_TT implements Runnable{
    //图片传输完成的标志位
    private final byte[] finishFlag={0x33,0x22,0x11};
    private static int transmitfinish=0;
    @Override
    public void run() {
        System.out.println("服务器启动，开始监听8888端口...");
        try(ServerSocket serverSocket = new ServerSocket(8000);
            //监听8888端口，获与取客户端对应的Socket对象
            Socket accept = serverSocket.accept();
            OutputStream socketWriter = accept.getOutputStream();//字节输出流
        ) {
            System.out.println("连接成功!");
            while (true){
                if (publicList.flag) {
                    //传输到ESP8266中
                    socketWriter.write(publicList.imagesData);

                    socketWriter.write(finishFlag);
                    publicList.flag=false;
                    socketWriter.flush();
                }


            }

        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("创建服务器失败");
        }
    }
}
